package com.example.designloginandregister;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Modify extends AppCompatActivity {
    private EditText modify_UserName, modify_OldPasswd, modify_NewPasswd1, modify_NewPasswd2;
    private Button ensureModify_btn;
    String userName, OldPasswd, NewPasswd1, NewPasswd2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);
        //为各个控件建立对象
        modify_UserName = findViewById(R.id.et_ModifyUserName);
        modify_OldPasswd = findViewById(R.id.et_Modify_OldPasswd);
        modify_NewPasswd1 = findViewById(R.id.et_Modify_NewPasswd1);
        modify_NewPasswd2 = findViewById(R.id.et_Modify_NewPasswd2);
        ensureModify_btn = findViewById(R.id.EnsureModify_btn);
        //为确认修改按钮建立点击事件
        ensureModify_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取所有EditText输入框里的内容
                userName = modify_UserName.getText().toString().trim();
                OldPasswd = modify_OldPasswd.getText().toString().trim();
                NewPasswd1 = modify_NewPasswd1.getText().toString().trim();
                NewPasswd2 = modify_NewPasswd2.getText().toString().trim();
                //开始判断
                if (TextUtils.isEmpty(OldPasswd)) {//旧密码
                    Toast.makeText(Modify.this, "请输入旧密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(NewPasswd1)) {//新密码
                    Toast.makeText(Modify.this, "请输入新密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(NewPasswd2)) {//两次新密码对比确认
                    Toast.makeText(Modify.this, "请再次输入新密码确认", Toast.LENGTH_SHORT).show();
                    return;
                } else if (!NewPasswd1.equals(NewPasswd2)) {//对比两次新密码
                    Toast.makeText(Modify.this, "再次输入的新密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                } else if (!MD5Util.md5(OldPasswd).equals(readPsw(userName))) {//对比原密码
                    Toast.makeText(Modify.this, "原密码一致,请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                } else if (MD5Util.md5(NewPasswd1).equals(readPsw(userName))) {//对比新原密码   出错!!!!
                    Toast.makeText(Modify.this, "新密码与原密码一致,请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                } else {//修改成功
                    Toast.makeText(Modify.this, "新密码设置成功", Toast.LENGTH_SHORT).show();
                    modifyPassw(NewPasswd1);
                    Intent intent = new Intent(Modify.this, MainActivity.class);
                    startActivity(intent);
                    Modify.this.finish();
                }
            }
        });


    }

    private String readPsw(String userName) {
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        return sp.getString(userName, "");
    }

    private void modifyPassw(String newPasswd1) {
        //加密保存新密码
        String md5psw = MD5Util.md5(newPasswd1);
        SharedPreferences sp = getSharedPreferences("loginInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(userName, md5psw);
        editor.commit();
    }
}
